﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ArgusLight.Fractals2D;
using System.IO;

namespace Fractals
{
	public partial class ExportImageWindow : ChildWindow
	{
		public delegate void ExportRequestedHandler(Type FractalType, Type ColoringType, FractalParameter fParameter, Stream stream);

		public FractalParameter FractalParameter { get; private set; }
		public Fractal fractal;
		public Fractal Fractal { get { return this.fractal.CreateNewInstance(); } }
		public SaveFileDialog SaveFileDialog { get; private set; }

		public Size MaxPreviewSize { get; set; }
		private FractalParameter para;

		public ExportImageWindow()
		{
			InitializeComponent();
			this.MaxPreviewSize = new Size(100, 100);
			this.SaveFileDialog = new SaveFileDialog();
			this.SaveFileDialog.DefaultExt = "png";
			this.SaveFileDialog.Filter = "Image (*.png)|*.png";
		}

		public void Show(Type FractalType, Type ColoringType, FractalParameter fParameter)
		{
			this.fractal = (Fractal)Activator.CreateInstance(FractalType);
			this.fractal.SetColoring((Coloring)Activator.CreateInstance(ColoringType));
			this.fractal.CalculationFinished += fractal_CalculationFinished;
			this.nudWidth.Value = fParameter.PixelWidth;
			this.nudHeight.Value = fParameter.PixelHeight;
			this.para = fParameter.GetCopy();
			double ratio = fParameter.PixelWidth/(double)fParameter.PixelHeight;
			Size s = this.MaxPreviewSize.ChangeRatio(ratio, System.Windows.ExtensionMethods.ChangeRatioOptions.DecreaseAreaSize);
			FractalParameter para = fParameter.GetCopy();
			para.PixelWidth = (int)s.Width;
			para.PixelHeight = (int)s.Height;
			this.fractal.StartCalculation(para);
			base.Show();
		}

		void fractal_CalculationFinished(object sender, CalculationFinishedArgs e)
		{
			this.image.Source = this.fractal.GetImage();
		}

		private void bCalculatePreview_Click(object sender, RoutedEventArgs e)
		{
			double ratio = this.nudWidth.Value / this.nudHeight.Value;
			Size s = this.MaxPreviewSize.ChangeRatio(ratio, System.Windows.ExtensionMethods.ChangeRatioOptions.DecreaseAreaSize);
			FractalParameter para = this.para.GetCopy(); ;
			para.PixelWidth = (int)s.Width;
			para.PixelHeight = (int)s.Height;
			this.fractal.StartCalculation(para);
		}

		private void bCancel_Click(object sender, RoutedEventArgs e)
		{
			this.DialogResult = false;
			//this.Close();
		}

		private void bSave_Click(object sender, RoutedEventArgs e)
		{
			if (this.SaveFileDialog.ShowDialog() == false)
				return;
			this.FractalParameter = this.para.GetCopy();
			this.FractalParameter.PixelWidth = (int)this.nudWidth.Value;
			this.FractalParameter.PixelHeight = (int)this.nudHeight.Value;
			this.DialogResult = true;
			//this.Close();
		}
	}
}

